use crate::app::UIComponent;
use eframe::egui;
use eframe::egui::{Align, Layout, Ui, Visuals};
use egui::MenuBar;

#[derive(Default)]
pub struct TopMenu {
    dark: bool,
}

impl TopMenu {
    pub fn new() -> TopMenu {
        Self::default()
    }
}

impl UIComponent for TopMenu {
    fn update(&mut self, ui: &mut Ui) {
        // 顶部菜单页
        egui::TopBottomPanel::top(ui.next_auto_id()).show_inside(ui, |ui| {
            MenuBar::new().ui(ui, |ui| {
                ui.with_layout(Layout::left_to_right(Align::LEFT), |ui| {
                    ui.menu_button("文件", |ui| {
                        ui.vertical_centered(|ui| {
                            ui.label("file1");
                            ui.label("file2");
                            ui.label("file3");
                            ui.label("file4");
                        });
                    });
                    ui.menu_button("帮助", |ui| {
                        ui.vertical_centered(|ui| {
                            ui.label("help1");
                            ui.label("help2");
                            ui.label("help3");
                            ui.label("help4");
                        });
                    });
                });

                ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
                    let (theme, text) = if self.dark {
                        (Visuals::dark(), "🌙")
                    } else {
                        (Visuals::light(), "🌞")
                    };
                    ui.ctx().set_visuals(theme);
                    if ui.button(text).clicked() {
                        self.dark = !self.dark;
                    }
                });
            });
        });
    }
}
